package 常用类.Arrays类;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Arrays01 {
    public static void main(String[] args) {
        Integer[] integer = {1,3,23,9};
        //显示数组Arrays.toString()
        //System.out.println(Arrays.toString(integer));

        //sort小到大排序， 也可以通过传入接口Comparator实现定制排序
        Integer[] integers1 = {1,5,3,8,12,45};
        Arrays.sort(integers1);
        System.out.println(Arrays.toString(integers1));

        Arrays.sort(integers1, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //return o1 - o2;从小到大
                return o2 - o1;//从大到小
            }
        });
        System.out.println(Arrays.toString(integers1));
        int[] ints = {1,8,3,12,67,23,98};
        sort001(ints, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                int o11 = (Integer)o1;
                int o22 = (Integer)o2;
                return o11 -o22;
            }
        });

        Integer[] s1 = {1,3,5,7,12,56};
        //binarySearch 通过二分查找元素,找到返回其对应的索引，要求必须是排好序的，如果不存在，返回 -(low + 1)索引
        int i = Arrays.binarySearch(s1, 58);
        System.out.println(i);

        //copyOf，数组拷贝,如果拷贝长度小于0，会抱错
        Integer[] s2 = Arrays.copyOf(s1, s1.length);
        System.out.println(Arrays.toString(s2));

        //Arrays.equals(),比较两个数组元素是否一样
        Integer[] s3 = {1,3,5,7,12,56};
        System.out.println(Arrays.equals(s1,s3));

        //asList，将数据转换成List集合
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);


    }
    //定制排序,冒泡排序
    public static void sort001(int[] ints, Comparator c){
        int temp = 0;
        for (int i = 0; i < ints.length -1; i++) {
            for (int j = 0; j < ints.length - 1 - i; j++) {
                if(c.compare(ints[j],ints[j + 1]) < 0){
                    temp = ints[j];
                    ints[j] = ints[j + 1];
                    ints[j + 1] = temp;
                }
            }

        }
        System.out.println(Arrays.toString(ints));
    }
}
